package com.example.spring_xx_u_market;

import com.alibaba.fastjson.JSONArray;
import com.example.bean.GoodsBriefV;
import com.example.bean.UidInfo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import util.UtilTime;

import java.util.ArrayList;
import java.util.List;

import static com.example.spring_xx_u_market.SpringXxUMarketApplication.jdbcTemplate;

/**
 * Json返回数据的Controller 测试 案例
 */

@RestController
@RequestMapping("test")
public class testClass {
    @RequestMapping("object")
    public UidInfo getTestUid() {
        UidInfo uidInfo = new UidInfo();
        uidInfo.setUid("test");
        uidInfo.setAccount(123456);
        uidInfo.setTime(UtilTime.getStringTimeNow());
        return uidInfo;
    }

    @RequestMapping("list")
    public List<UidInfo> getTestUidList() {
        ArrayList<UidInfo> uidInfos = new ArrayList<>();
        UidInfo uidInfo = new UidInfo();
        uidInfo.setUid("test");
        uidInfo.setAccount(123456);
        uidInfo.setTime(UtilTime.getStringTimeNow());
        uidInfos.add(uidInfo);
        uidInfos.add(uidInfo);
        return uidInfos;
    }

    public class test {
        private int i;
        private String a;
        private JSONArray tt;
        private Object aa;

        public int getI() {
            return i;
        }

        public void setI(int i) {
            this.i = i;
        }

        public String getA() {
            return a;
        }

        public void setA(String a) {
            this.a = a;
        }

        public JSONArray getTt() {
            return tt;
        }

        public void setTt(JSONArray tt) {
            this.tt = tt;
        }

        public Object getAa() {
            return aa;
        }

        public void setAa(Object aa) {
            this.aa = aa;
        }

        test() {
            aa = new UidInfo();
            tt = new JSONArray();
            UidInfo uidInfo = new UidInfo();
            uidInfo.setUid("test");
            uidInfo.setAccount(123456);
            uidInfo.setTime(UtilTime.getStringTimeNow());
            tt.add(uidInfo);
            tt.add(uidInfo);
        }
    }

    /**
     * Json返回复杂嵌套的数据的Controller 测试 案例
     */
    @RequestMapping("test")
    public test getTest() {
        test test = new test();
        return test;
    }

    /**
     * 返回字符串测试
     */

    @RequestMapping(value = "str", produces = "text/html;charset=UTF-8")
    public String getStr() {
        return "000000";
    }

    /**
     * 数据库测试
     */
    @RequestMapping("sql")
    public GoodsBriefV getsql() {
        List<GoodsBriefV> query = jdbcTemplate.query("select goods_id, name,image_urls,type,low,high,during_time,currency_type from second_hand.goods_info,second_hand.price_info where account = ? and price_id = goods_info.price_info_id",
                new BeanPropertyRowMapper<>(GoodsBriefV.class), 123456);
        return query.get(0);
    }

    @RequestMapping("limit")
    public List<UidInfo> sqlLimit() {
        List<UidInfo> query = jdbcTemplate.query("select * from second_hand.uid limit ?,?",
                new BeanPropertyRowMapper<>(UidInfo.class), 1, 20);
        return query;
    }
}
